package stai6271;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.thrift.TException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Modified code from http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html
 *   Currently only prints to console
 * 
 * @author Nel
 *
 */
public class XmlSax extends DefaultHandler {
	
	private String temp;
	private String key, LAC, ID, Body, Score;
	private int keyCount = 0;
	static Cassandra.Client cli = null;

	/** The main method sets things up for parsing */
	public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
	 	
		//Create a "parser factory" for creating SAX parsers
		SAXParserFactory spfac = SAXParserFactory.newInstance();

		//Now use the parser factory to create a SAXParser object
		SAXParser sp = spfac.newSAXParser();

		//Create an instance of this class; it defines all the handler methods
		XmlSax handler = new XmlSax();
		
		cli = ClientManager.getConnection();
		try {
			cli.set_keyspace("bicycle");
		} catch (InvalidRequestException | TException e) {
			System.out.println("Unable to set keyspace");
			e.printStackTrace();
		}

        //Finally, tell the parser to parse the input and notify the handler
        sp.parse("bicyclePosts.xml", handler);
      
        ClientManager.closeConnection();
	}

    /**
    * When the parser encounters plain text (not XML elements),
    * it calls(this method, which accumulates them in a string buffer
    */
    public void characters(char[] buffer, int start, int length) {
    	temp = new String(buffer, start, length);
    }
      

       /*
        * Every time the parser encounters the beginning of a new element,
        * it calls this method, which resets the string buffer
        *   This is where we call the program to insert data to the database. 
        */ 
       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
              temp = "";
              if (qName.equalsIgnoreCase("row")) {
            	  //Create user_key_ID
            	  ++keyCount;
            	  key = new String("bicycle" + keyCount);
            	  
            	  //Get column values
                  LAC =  new String(attributes.getValue("LastActivityDate"));  
                  ID = new String(attributes.getValue("Id"));
                  Body = new String(attributes.getValue("Body"));
                  Score = new String(attributes.getValue("Score"));
            	  
                  try {
					ClientManager.insertRow(cli, key, LAC, ID, Body, Score);
				} catch (UnsupportedEncodingException e) {
					System.out.println("Unable to set column value");
					e.printStackTrace();
				}
            	  System.out.println("User_Key_ID: " + key + " Last Activity Date: " +  LAC +
                                 " ID: " +  ID + " Body: " + Body +
                                   " Score: " + Score);
              }
       }

       /*
        * When the parser encounters the end of an element, it calls this method
        */
       public void endElement(String uri, String localName, String qName)
                     throws SAXException {
           //Do nothing for now            
       }
      
}